package net.ciklum.svnnotify.svn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.tmatesoft.svn.core.SVNAuthenticationException;
import org.tmatesoft.svn.core.SVNCancelException;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.util.SVNHashMap;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import android.content.Context;
import android.os.AsyncTask;

//SVNRepository from SVNkit
public class SVNRepoConnection {
	
	private static final int END_REVISION = -1;
	private static final int START_REVISION = 0;
	private String uri;
	private String login;
	private String password;
	private String name;
	private long id;
	private SVNRepository repository;
	private List<SVNNotification> notifications;
	
	public SVNRepoConnection(String uri, String name) throws SVNException {
		setUri(uri);
		setName(name);
		setupRepoConnection();
	} 
	
	public SVNRepoConnection(String uri, String name, long id) throws SVNException {
		this(uri, name);
		setId(id);
	} 
	
	public SVNRepoConnection(String uri, String login, String password) throws SVNException {
		setUri(uri);
		setLogin(login);
		setPassword(password);
		setupRepoConnection();
	}
	
	public SVNRepoConnection(String uri, String login, String password,  String name, long id) throws SVNException {
		this(uri, login, password);
		setName(name);
		setId(id);
	}

	public void setUri(String uri) {
		this.uri = uri;
	}
	public String getUri() {
		return uri;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getLogin() {
		return login;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}
	
	public void setNotifications(List<SVNNotification> notifications) {
		this.notifications = notifications;
	}

	public List<SVNNotification> getNotifications() {
		return notifications;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}
	
	public void setupRepoConnection() throws SVNException {
				DAVRepositoryFactory.setup();
		        SVNRepositoryFactoryImpl.setup();
		        FSRepositoryFactory.setup();
		        repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(uri));
                if(login != null & password != null) {
                	 ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(login, password);
                	 repository.setAuthenticationManager(authManager);
                }               
                repository.testConnection();
	}
	
	public List<SVNNotification> getLatestRevisions() throws SVNException  {
            repository.testConnection();
            Collection<SVNLogEntry> logEntries = null;
            logEntries = repository.log(new String[] { }, 
            							null,
            							START_REVISION, 
            							END_REVISION, 
            							true,
            							true);
            if(!logEntries.isEmpty()) {
            	setNotifications(new ArrayList<SVNNotification>());
            	for (SVNLogEntry entry : logEntries) {
            		getNotifications().add(new SVNNotification(entry.getRevision(),
            											  entry.getAuthor(),
            											  entry.getChangedPaths(),
            											  entry.getDate(),
            											  entry.getMessage()));
				}
            }
			return getNotifications();
	}
	
	public long getLatestRevision() throws SVNException {
		return repository.getLatestRevision();
	}

}
